package com.wbao.dianniu.logical;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author yejianjun
 */
public class SharedPreferencesUtils {

    private static final String MY_SETTTING_PREFERENCES = "my_settting_share_preferences";
    /**
     * 是否为第一次打开app
     */
    private static final String APPFIRSTOPEN_PREFERENCES = "app_first_open_preferences";
    private static SharedPreferences mSettingPreferences;
    private static final String AUTO_LOGIN_PREFERENCES = "auto_login_preferences";
    private static final String USER_NAME_PREFERENCES = "user_name_preferences";
    private static final String PASSWORD_PREFERENCES = "password_preferences";
    private static final String AUTO_UPDATE_TIME = "auto_update_time";
    private static final String LOGIN_ERROR_COUNT = "login.error.count";
    private static final String LAST_LOGIN_TIME = "last_login_time";
    private static final String LONGITUDE_PREFERENCES = "preferences_longitude";
    private static final String LATITUDE_PREFERENCES = "preferences_latitude";
    private static final String TOKEN_PREFERENCES = "token_preferences";
    private static final String CDNTOKEN_PREFERENCES = "cdntoken_preferences";
    //    private static final String KNOWLEDGE_HIS_STATUS = "knowledge_his_status";
//    private static final String SHARE_PREFERENCE_NAME = "EmotionKeyboard";
    private static final String SHARE_PREFERENCE_SOFT_INPUT_HEIGHT = "soft_input_height";
    private static final String SHARE_PREFERENCE_CLIENTID = "getui_clientId";


    public static void init(Context context) {
        mSettingPreferences = context.getSharedPreferences(MY_SETTTING_PREFERENCES, Activity.MODE_PRIVATE);
    }

    public static void relese() {
        mSettingPreferences = null;
    }

    public static void clear() {
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(APPFIRSTOPEN_PREFERENCES, null);
        editor.putString(AUTO_LOGIN_PREFERENCES, null);
        editor.commit();
    }

    @SuppressLint("NewApi")
    public static boolean getIsFirstOpen(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getBoolean(APPFIRSTOPEN_PREFERENCES, true);
    }

    @SuppressLint("NewApi")
    public static void setIsFirstOpen(Context context, boolean isFirstOpen) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putBoolean(APPFIRSTOPEN_PREFERENCES, isFirstOpen);
        editor.commit();
    }

    /**
     * 设置自动登录状态
     *
     * @param auto
     */
    public static void setAutoLoginStatus(Context context, boolean auto) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putBoolean(AUTO_LOGIN_PREFERENCES, auto);
        editor.commit();
    }


    /**
     * 获取自动登录状态
     *
     * @return
     */
    public static boolean getAutoLoginStatus(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getBoolean(AUTO_LOGIN_PREFERENCES, false);
    }
    /**
     *设置用户ID
     *
     * @param username
     */
//	public static void setAccountId(String accountId){
//		SharedPreferences.Editor editor = mSettingPreferences.edit();
//		editor.putString(USER_ACCOUTID_PREFERENCES, accountId);
//		editor.commit();
//	}

    /**
     * 获取用户ID
     *
     * @return
     */
//	public  static String getAccountId(){
//		return mSettingPreferences.getString(USER_ACCOUTID_PREFERENCES, "");
//	}

    /**
     * 设置用户名，登录后台时的手机号码
     *
     * @param context
     * @param username
     */
    public static void setUserName(Context context,String username) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(USER_NAME_PREFERENCES, username);
        editor.commit();
    }

    /**
     * 获取用户名，登录后台时的手机号码
     * @param context
     * @return
     */
    public static String getUserName(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(USER_NAME_PREFERENCES, "");
    }

    /**
     * 设置密码
     *
     *@param context
     * @param password
     */
    public static void setPassword(Context context, String password) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(PASSWORD_PREFERENCES, password);
        editor.commit();
    }

    /**
     * 获取密码
     *
     * @return
     */
    public static String getPassword(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(PASSWORD_PREFERENCES, "");
    }

    /**
     *
     * @param context
     * @param token
     */
    public static void setToken(Context context, String token) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(TOKEN_PREFERENCES, token);
        editor.commit();
    }

    public static String getToken(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(TOKEN_PREFERENCES, "");
    }

    public static void setCdnToken(Context context, String cdnToken) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(CDNTOKEN_PREFERENCES, cdnToken);
        editor.commit();
    }

    public static String getCdnToken(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(CDNTOKEN_PREFERENCES, "");
    }

    /**
     * 设置检测更新时间
     *
     * @param context
     * @param time
     */
    public static void setUpdateTime(Context context, long time) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putLong(AUTO_UPDATE_TIME, time);
        editor.commit();
    }

    /**
     * 获取上次检测更新时间
     *
     * @return
     */
    public static long getUpdateTime(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getLong(AUTO_UPDATE_TIME, 0);
    }

//	public static boolean getIsFirstStart(Context context){
//		if(null == mSettingPreferences){
//			mSettingPreferences =  context.getSharedPreferences(MY_SETTTING_PREFERENCES, Activity.MODE_PRIVATE);
//		}
//		return mSettingPreferences.getBoolean(IS_FIRST_START, true);
//	}

//	public static void setIsFirstStart(boolean status){
//		SharedPreferences.Editor editor = mSettingPreferences.edit();
//		editor.putBoolean(IS_FIRST_START, status);
//		editor.commit();
//	}

    /**
     * 获取登录失败次数
     *
     * @param context
     * @return
     */
    public static int getLoginErrorCount(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getInt(LOGIN_ERROR_COUNT, 0);
    }

    /**
     * 设置登录失败次数
     *
     * @param context
     * @param count
     */
    public static void setLoginErrorCount(Context context, int count) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putInt(LOGIN_ERROR_COUNT, count);
        editor.commit();
    }

    /**
     * 获取上次登录时间
     *
     * @param context
     * @return
     */
    public static long getLastLoginTime(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getLong(LAST_LOGIN_TIME, 0);
    }

    /**
     * 设置上次登录时间
     *
     * @param context
     * @param time
     */
    public static void setLastLoginTime(Context context,long time) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putLong(LAST_LOGIN_TIME, time);
        editor.commit();
    }

    /**
     * @param context
     * @return
     */
    public static String getLongitude(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(LONGITUDE_PREFERENCES, null);
    }

    /**
     * 设置精度
     *
     * @param context
     * @param longitude
     */
    public static void setLongitude(Context context, String longitude) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(LONGITUDE_PREFERENCES, longitude);
        editor.commit();
    }

    /**
     * @param context
     * @return
     */
    public static String getLatitude(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(LATITUDE_PREFERENCES, null);
    }

    /**
     * 设置纬度
     *
     * @param context
     * @param latitude
     */
    public static void setLatitude(Context context,String latitude) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(LATITUDE_PREFERENCES, latitude);
        editor.commit();
    }

//    /**
//     * 得到知识分享草稿状态 true 保存了草稿 false
//     * @param context
//     * @return
//     */
//    public static boolean getKnowHisStatus(Context context) {
//        if (null == mSettingPreferences) {
//            mSettingPreferences = context.getSharedPreferences(MY_SETTTING_PREFERENCES, Activity.MODE_PRIVATE);
//        }
//        return mSettingPreferences.getBoolean(KNOWLEDGE_HIS_STATUS, false);
//    }
//
//    /**
//     * 设置知识分享草稿状态  true 保存了草稿 false 不保存
//     *
//     * @param status
//     */
//    public static void setKnowHisStatus(boolean status) {
//        SharedPreferences.Editor editor = mSettingPreferences.edit();
//        editor.putBoolean(KNOWLEDGE_HIS_STATUS, status);
//        editor.commit();
//    }

    /**
     * 获取软键盘高度
     *
     * @param context
     * @return
     */
    public static int getKeyboardHeight(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getInt(SHARE_PREFERENCE_SOFT_INPUT_HEIGHT, 0);
    }

    /**
     * 设置软键盘高度
     *
     * @param height
     */
    public static void setKeyboardHeight(Context context, int height) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putInt(SHARE_PREFERENCE_SOFT_INPUT_HEIGHT, height);
        editor.commit();
    }

    /**
     * 获取个推ID
     *
     * @param context
     * @return
     */
    public static String getClientId(Context context) {
        if (null == mSettingPreferences) {
            init(context);
        }
        return mSettingPreferences.getString(SHARE_PREFERENCE_CLIENTID, null);
    }

    /**
     * 设置个推ID
     *
     * @param context
     * @param clientId
     */
    public static void setClientId(Context context, String clientId) {
        if (null == mSettingPreferences) {
            init(context);
        }
        SharedPreferences.Editor editor = mSettingPreferences.edit();
        editor.putString(SHARE_PREFERENCE_CLIENTID, clientId);
        editor.commit();
    }


}
